package com.ag.lwc.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.Session;

import com.ag.bean.TbLog;
import com.ag.bean.TbLogPlanStep;
import com.ag.dao.TbEventInfectDAO;
import com.ag.dao.TbLogDAO;

//查询日志的dao,
public class MyLogDAO extends TbLogDAO {
	private static final Log log = LogFactory.getLog(TbEventInfectDAO.class);

	// 根据环节的id查询该环节的所有日志，适用于查询该环节的历史环节
	public List<TbLog> find(String psId,String maxTime) {
		System.out.println("services: 根据环节id：" + psId + " 查询某一环节的所有日志");
		if (psId == null)
			return null;
		Session session = this.getSessionFactory().openSession();
//		TbLogPlanStep planStep = (TbLogPlanStep) session.get(
//				TbLogPlanStep.class, psId);
		List<TbLog> result = session
				.createQuery(
						"from TbLog log left join fetch log.tbLogPlanStep step where step.psId =:psId order by log.logId")
				.setString("psId", psId).list();
//		if (result != null)
//			for (TbLog log : result) {
//				log.getLogContent();
//				log.getTbLogType().getLtName();
//				log.getLogTime();
//				System.out.println("日志类型：" + log.getTbLogType().getLtName()
//						+ " 日志内容：" + log.getLogContent());
//			}
		session.close();
		return result;

	}

	public List<TbLog> find_bak(String psId, String afterLogId) {
		// System.out.println("根据环节id："+psId+" 查询某一环节的已经更新了的日志记录日志: "+afterLogId);
		if (psId == null)
			return null;
		Session session = this.getSessionFactory().openSession();
		TbLogPlanStep logplanstep = (TbLogPlanStep) session.get(
				TbLogPlanStep.class, psId);
		List<TbLog> result = session
				.createQuery(
						"from TbLog log left join fetch log.tbLogPlanStep step left join fetch log.tbLogType where step.psId =:psId and log.logId > :afterLogId order by log.logId asc")
				.setString("afterLogId", afterLogId).setString("psId", psId)
				.list();
		/*
		 * for(TbLog log:result) { Hibernate.initialize(log.getTbLogType());
		 * Hibernate.initialize(log.getTbPlan());
		 * Hibernate.initialize(log.getTbLogPlanStep()); }
		 */
		session.close();
		return result;

	}
	
	public List findNew(String hql) {
		log.debug("finding new TbLog instances");
		try {
			return getHibernateTemplate().find(hql);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

}
